home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransSkel / NewStuff ƒ / NewStuff.p < prev    next >
Encoding:
Text File  |  1995-01-02  |  8.8 KB  |  374 lines  |  [TEXT/PJMM]

  1. {NewStuff by Ingemar Ragnemalm}
  2. {}
  3. {This is an extra demo that I have made for TransSkel, in order to demonstrate some of the}
  4. {new features.}
  5. {}
  6. {Some features are yet not demonstrated, namely how to handle Apple Events and how to use}
  7. {the mouse region in WaitNextEvent.}
  8.  
  9. program NewStuff;
  10.  
  11.     uses
  12. {$IFC UNDEFINED THINK_PASCAL}
  13.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, GestaltEqu, 
  14. {$ENDC}
  15.         Picker, DialogUtils, TransSkel;
  16.  
  17.     const
  18.         kFileMenuRes = 128;        {MENU resource id}
  19.         kHierMenuRes = 129;    {MENU resource id}
  20.         kDlogRes = 128;            {DLOG resource id}
  21.         kAboutRes = 129;        {ALRT resource id}
  22.         kMessageRes = 130;        {ALRT resource id}
  23.  
  24.     var
  25.         fileMenu, hierMenu: MenuHandle;
  26.         theDialog: DialogPtr;
  27.         theColor: RGBColor;
  28.         gR: Rect; {Used for a silly nullEvent animation}
  29.         gColorFlag: Boolean;
  30.         gMouseRgn1, gMouseRgn2, gMouseRgn3: RgnHandle;
  31.  
  32. {Dialog items}
  33.     var
  34.         defaultItem: integer; {Either changeButton or beepButton}
  35.     const
  36. {1: Stat text}
  37.         userItem = 2;
  38.         changeButton = 3;
  39.         beepButton = 4;
  40.         changeRadio = 5;
  41.         beepRadio = 6;
  42. {7: Stat text}
  43.         editText = 8;
  44.  
  45.  
  46. {Respond to selection of "About"}
  47.     procedure DoAbout;
  48.     begin
  49.         if Alert(kAboutRes, nil) = 1 then
  50.             ;
  51.     end;
  52.  
  53.     procedure DoFileMenu (item: integer);
  54.     begin
  55.         case item of
  56.             1: 
  57.                 if 1 = Alert(kMessageRes, nil) then
  58.                     ;
  59. {2 is the hierarcical item, which works automatically}
  60.             4: 
  61.                 SkelWhoa;
  62.             otherwise
  63.                 SkelWhoa;
  64.         end{case}
  65.     end;
  66.  
  67.     procedure DoHierMenu (item: integer);
  68.         var
  69.             savePort: GrafPtr;
  70.     begin
  71.         case item of
  72.             1: 
  73.                 begin
  74.                     theColor.red := -1;
  75.                     theColor.green := 0;
  76.                     theColor.blue := 0;
  77.                 end;
  78.             2: 
  79.                 begin
  80.                     theColor.red := 0;
  81.                     theColor.green := -1;
  82.                     theColor.blue := 0;
  83.                 end;
  84.             3: 
  85.                 begin
  86.                     theColor.red := 0;
  87.                     theColor.green := 0;
  88.                     theColor.blue := -1;
  89.                 end;
  90.         end; {case}
  91.  
  92. {Force an update on the user item rectngle}
  93.         GetPort(savePort);
  94.         SetPort(theDialog);
  95.         InvalRect(GetDItemBox(theDialog, userItem));
  96.         SetPort(savePort);
  97.     end;
  98.  
  99. {    Initialize menus.  Tell TransSkel to process the Apple menu}
  100. {    automatically, and associate the proper procedures with the}
  101. {    File and Edit menus.}
  102.  
  103.     procedure SetUpMenus;
  104.     begin
  105.         SkelApple('About NewStuff…', @DoAbout);
  106.         fileMenu := GetMenu(kFileMenuRes);
  107.         if SkelMenu(fileMenu, @DoFileMenu, nil, true) then
  108.             ;                                        {Tell TransSkel to handle the menu}
  109.         hierMenu := GetMenu(kHierMenuRes);
  110.         if SkelHMenu(hierMenu, @DoHierMenu, nil) then
  111.             ;                                        {Tell TransSkel to handle the hierarcical menu}
  112.     end;
  113.  
  114. {Routines for the modeless dialog window}
  115.  
  116.     procedure DoChange;
  117.         var
  118.             savePort: GrafPtr;
  119.     begin
  120.         if gColorFlag then
  121.             begin
  122.                 if GetColor(Point($00400040), 'Select a new color', theColor, theColor) then
  123.                     begin
  124. {Force an update on the user item rectngle}
  125.                         GetPort(savePort);
  126.                         SetPort(theDialog);
  127.                         InvalRect(GetDItemBox(theDialog, userItem));
  128.                         SetPort(savePort);
  129.                     end;
  130.             end
  131.         else
  132.             SysBeep(1);
  133.     end;
  134.  
  135.     procedure DoEnter;
  136.         var
  137.             l: Longint;
  138.     begin
  139.         HighlightDItem(theDialog, defaultItem);
  140.         Delay(2, l);
  141.         EnableDItem(theDialog, defaultItem);    {Un-highlight}
  142.         if defaultItem = changeButton then
  143.             begin
  144.                 DoChange;
  145.             end
  146.         else if defaultItem = beepButton then
  147.             begin
  148.                 SysBeep(1);
  149.             end;
  150.     end;
  151.  
  152. {Handle events in the dialog. We only bother with keydown events here.}
  153.  
  154.     procedure Event (itemNum: integer; theEvent: EventRecord);
  155.         var
  156.             box, r1, r2: Rect;
  157.     begin
  158.         if theEvent.what = mouseDown then
  159.             case itemNum of
  160.                 changeButton: 
  161.                     DoChange;
  162.                 beepButton: 
  163.                     SysBeep(1);
  164.                 changeRadio: 
  165.                     begin
  166.                         SetBooleanDItem(theDialog, changeRadio, true);
  167.                         SetBooleanDItem(theDialog, beepRadio, false);
  168.                         defaultItem := changeButton;
  169.                         r1 := GetDItemBox(theDialog, changeButton);
  170.                         r2 := GetDItemBox(theDialog, beepButton);
  171.                         UnionRect(r1, r2, r1);
  172.                         InsetRect(r1, -4, -4);
  173.                         InvalRect(r1);
  174.                     end;
  175.                 beepRadio: 
  176.                     begin
  177.                         SetBooleanDItem(theDialog, changeRadio, false);
  178.                         SetBooleanDItem(theDialog, beepRadio, true);
  179.                         defaultItem := beepButton;
  180.                         r1 := GetDItemBox(theDialog, changeButton);
  181.                         r2 := GetDItemBox(theDialog, beepButton);
  182.                         UnionRect(r1, r2, r1);
  183.                         InsetRect(r1, -4, -4);
  184.                         InvalRect(r1);
  185.                     end;
  186.                 otherwise
  187.             end; {case}
  188.  
  189.     end; {Event}
  190.  
  191.     procedure Filter (theDialog: DialogPtr; var theEvent: EventRecord; var result: Boolean);
  192.         var
  193.             theKey: Char;
  194.             box: Rect;
  195.             saveColor: RGBColor;
  196.     begin
  197.         result := false;
  198.  
  199. {If we want to filter out crtain events, we can do it here. In our case, we filter out}
  200. {return and enter, which now activates the default button rather than sending them}
  201. {to the edit box.}
  202.  
  203.         if (theEvent.what = keyDown) or (theEvent.what = autoKey) then
  204.             begin
  205.                 theKey := char(BitAnd(theEvent.message, charCodeMask));
  206.                 if (ord(theKey) = 13) or (ord(theKey) = 3) then
  207.                     begin
  208. {Filter out the keydown so it won't go into the edit box.}
  209.                         DoEnter;
  210.                         result := true;
  211.                     end;
  212.             end;
  213.  
  214. {If we have special items - in this case a user item and a frame around a button - we}
  215. {must handle the update event in the filter function. If we don't, DialogSelect will handle}
  216. {it for us, inside TransSkel, and just update all standard items.}
  217.  
  218.         if theEvent.what = updateEvt then
  219.             if theDialog = WindowPtr(theEvent.message) then
  220.                 begin
  221.                     BeginUpdate(theDialog);
  222.                     SetPort(theDialog);
  223.                     EraseRect(theDialog^.portRect);
  224.  
  225.                     DrawDialog(theDialog);
  226.  
  227.                     box := GetDItemBox(theDialog, defaultItem);
  228.                     InsetRect(box, -4, -4);
  229.                     PenSize(3, 3);
  230.                     FrameRoundRect(box, 15, 15);
  231.  
  232.                     box := GetDItemBox(theDialog, userItem);
  233.                     if gColorFlag then
  234.                         begin
  235.                             GetForeColor(saveColor);
  236.                             RGBForeColor(theColor);
  237.                             PaintRect(box);
  238.                             RGBForeColor(saveColor);
  239.                         end;
  240.  
  241.                     EndUpdate(theDialog);
  242.                     result := true;
  243.                 end;
  244.  
  245.     end; {Filter}
  246.  
  247.  
  248.     procedure CalcRegions;
  249.         var
  250.             p: Point;
  251.     begin
  252. {We need three regions:}
  253. {1) the editable text box}
  254. {2) the window except the edit text}
  255. {3) everything else}
  256.  
  257.         gMouseRgn1 := NewRgn;
  258.         gMouseRgn2 := NewRgn;
  259.         gMouseRgn3 := NewRgn;
  260.         RectRgn(gMouseRgn1, GetDItemBox(theDialog, editText));
  261.         RectRgn(gMouseRgn2, theDialog^.portRect);
  262.         SetPt(p, 0, 0);
  263.         LocalToGlobal(p);
  264.         OffsetRgn(gMouseRgn1, p.h, p.v);
  265.         OffsetRgn(gMouseRgn2, p.h, p.v);
  266.         CopyRgn(GetGrayRgn, gMouseRgn3);
  267.         DiffRgn(gMouseRgn3, gMouseRgn2, gMouseRgn3);
  268.         DiffRgn(gMouseRgn2, gMouseRgn1, gMouseRgn2);
  269.         SkelSetMouseRgn(gMouseRgn1);
  270.     end;
  271.  
  272.     procedure SetupDialog;
  273.     begin
  274.         theDialog := GetNewDialog(kDlogRes, nil, WindowPtr(-1));
  275.         if SkelDialog(theDialog, @Event, nil, nil, @Filter) then
  276.             ;
  277.  
  278.         gMouseRgn1 := NewRgn;
  279.         gMouseRgn2 := NewRgn;
  280.         gMouseRgn3 := NewRgn;
  281.         CalcRegions;
  282.  
  283. {Indicate the default button and set its radio button}
  284.         defaultItem := changeButton;
  285.         SetBooleanDItem(theDialog, changeRadio, true);
  286.  
  287. {Set up the little rectangle used for indicating that there is a background process}
  288.         gR := theDialog^.portRect;
  289.         gR.left := gR.right - 5;
  290.         gR.bottom := gR.top + 5;
  291.     end;
  292.  
  293. {MultiFinder events}
  294.  
  295.     procedure DoSuspendResume (isResume: Boolean);
  296.     begin
  297.         if isResume then
  298.             SkelSetSleep(2) {Resume}
  299.         else
  300.             SkelSetSleep(20); {Suspend}
  301.     end;
  302.  
  303.     procedure DoMouseMoved;
  304.         var
  305.             p: Point;
  306.     begin
  307.         GetMouse(p);
  308.         LocalToGlobal(p);
  309.         CalcRegions;
  310.         if PtInRgn(p, gMouseRgn1) then
  311.             begin
  312.                 SkelSetMouseRgn(gMouseRgn1);
  313.                 SetCursor(GetCursor(1)^^); {I-beam cursor, for text}
  314.             end
  315.         else if PtInRgn(p, gMouseRgn2) then
  316.             begin
  317.                 SkelSetMouseRgn(gMouseRgn2);
  318.                 SetCursor(GetCursor(128)^^); {Upside-down cursor}
  319.             end
  320.         else if PtInRgn(p, gMouseRgn3) then
  321.             begin
  322.                 SkelSetMouseRgn(gMouseRgn3);
  323. {$IFC UNDEFINED THINK_PASCAL}
  324.                 SetCursor(qd.arrow);
  325. {$ELSEC}
  326.                 SetCursor(arrow);
  327. {$ENDC}
  328.             end;
  329.     end;
  330.  
  331. {Background process, called on null events even when the program is switched out.}
  332.  
  333.     procedure Background;
  334.         var
  335.             savePort: GrafPtr;
  336.     begin
  337.         GetPort(savePort);
  338.         SetPort(theDialog);
  339.         EraseRect(gR);
  340.         OffsetRect(gR, 0, 1);
  341.         PaintRect(gR);
  342.         if gR.top > theDialog^.portRect.bottom then
  343.             OffsetRect(gR, 0, -theDialog^.portRect.bottom - 5);
  344.         SetPort(savePort);
  345.     end; {Background}
  346.  
  347. {Check if Color QuickDraw is available, with Gestalt. We assume that Gestalt is}
  348. {available - it has been there for a LONG time now, and they say that there is glue}
  349. {for it as well! Sadly, documentation for glue code is non-existant as far as I can tell.}
  350.  
  351.     procedure InitFlags;
  352.         var
  353.             feature: LongInt;
  354.     begin
  355.         gColorFlag := false;
  356.         if noErr = Gestalt(gestaltQuickdrawVersion, feature) then
  357.             gColorFlag := feature > 0; {0 = non-color QD}
  358.     end; {InitFlags}
  359.  
  360. {Main program}
  361.  
  362. begin
  363.     SkelInit(6, nil);                                        { Initialize                                }
  364.     SetUpMenus;
  365.     InitFlags;
  366.     SetupDialog;
  367.     SkelSetSuspendResume(@DoSuspendResume);
  368.     SkelSetMouseMoved(@DoMouseMoved);
  369.     SkelBackground(@Background);
  370.     SkelSetSleep(2);
  371.  
  372.     SkelMain;                                                { loop til quit selected                }
  373.     SkelClobber;                                            { clean up                                }
  374. end.